package com.huike.listview_space;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;

import androidx.appcompat.app.AppCompatActivity;

/**
 * ListView 拉下动画
 */
public class MainActivity extends AppCompatActivity {

    private ListView lv_main;
    private ArrayList<String> list;

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        list = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
             list.add(i+"");//添加数据
        }
        //设置设配器
        lv_main.setAdapter(new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,list));

        //监听listView 触摸事件
        lv_main.setOnTouchListener(new View.OnTouchListener() {

            private int dy;
            private float start;

            @Override
            public boolean onTouch(View view, MotionEvent ev) {

                switch (ev.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        start = ev.getY();
                        break;
                    case MotionEvent.ACTION_MOVE:

                        float end = ev.getY();
                        //偏移量
                        dy = (int) ((int) (end - start) * 0.3);


                        if(dy <= 0){
                            dy = 1;
                            return  false;//取消消费 让listview 滚动

                        }else if(dy >= 30){
                            dy = 30;
                        }

                        lv_main.setDividerHeight(dy);


                        break;
                    case MotionEvent.ACTION_UP:
                        lv_main.setDividerHeight(1);
                        break;
                }

                return true;
            }
        });


    }

    private void initView() {
        lv_main = (ListView) findViewById(R.id.lv_main);
    }
}
